package com.crishne.anwo.service.impl;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.crishne.anwo.dao.RoleMapDao;
import com.crishne.anwo.model.RoleMap;
import com.crishne.anwo.model.UserMap;
import com.crishne.anwo.service.RoleMapService;
import com.crishne.anwo.utill.PaginationUtil;

@Service
@Transactional
public class RoleMapServiceImpl implements RoleMapService {
	
	@Autowired
	private RoleMapDao roleMapDao;

	@Override
	public void addRoleMap(RoleMap roleMap) {
		roleMapDao.addRoleMap(roleMap);
	}

	@Override
	public List<RoleMap> getAllRoles() {
		return roleMapDao.getAllRoleMaps();
	}

	@Override
	public PaginationUtil<RoleMap> searchRoleMaps(
			PaginationUtil<RoleMap> pageConditions) {
		return roleMapDao.searchRoleMaps(pageConditions);
	}
	
	@Override
	public List<RoleMap> getRoleMapMenus(String roleId){
		return roleMapDao.getRoleMapMenus(roleId);
	}
    
	@Override
    public void saveMenuMaps(String roleId,String[] menuIds){
		//先删除所有所属角色
		roleMapDao.deleteRoleMapByRoleId(roleId);
		
		//添加选择角色
		for(int i=0;i<menuIds.length;i++){
			if(!"".equals(menuIds[i].trim())){
				RoleMap rm = new RoleMap();
				rm.setStRoleMapId(UUID.randomUUID().toString());
				rm.setStRoleId(roleId);
				rm.setStMenuId(menuIds[i]);
				roleMapDao.addRoleMap(rm);
			}
		}
	}
    
	@Override
    public void deleteRoleMapByRoleId(String stRoleId){
		roleMapDao.deleteRoleMapByRoleId(stRoleId);
	}

}
